<extend name="Layout/ins_page" />
<block name="content">
   <link href="/sap/demo.css" rel="stylesheet" type="text/css" />
    <script src="/sap/scripts/boot.js" type="text/javascript"></script>



        <legend><span>入库</span></legend>
        <div  id='fm1'>
        <div class="fieldset-body">
        供应商代码:  <div class="mini-autocomplete" style="width:250px;"  popupWidth="400" textField="cardcode" valueField="cardname"  id='CardCode' name='CardCode'
        url="/Warehouse/getSupplier" value="" text=""  onvaluechanged="" >     
        <div property="columns">
            <div header="客户代码" field="cardcode" width="30"></div>
            <div header="客户名称" field="cardname"></div>
        </div>
    </div>
    <!--  <span>添加日期：</span>
		<input id="start" class="mini-datepicker"  format="yyyy-MM-dd "  showTime="true" valueType='string' value="<?php echo date('Y-m-d',time()-86400*7)?>"/>
		<input id="end" class="mini-datepicker"  format="yyyy-MM-dd "  showTime="true" valueType='string' value="<?php echo date('Y-m-d ')?>" /> -->
		
	入库类型:<input id="s_type" class="mini-combobox" style="width:150px;" textField="text" valueField="id" emptyText="请选择..."
    data="[{id:'GIN1',text:'盘盈调拨入库'},{id:'OR',text:'退货入库'}]"  required="true" allowInput="true" showNullItem="true" nullItemText="请选择..."/>
	<br/>
    <!-- <a class="mini-button" onclick="search()" enabled="" id='query-bt'  style="margin-top: 5px">&nbsp;&nbsp;查询&nbsp;&nbsp;</a> -->
 
  
   
    <div id="tabs1" class="mini-tabs" activeIndex="0" style="width:100%;margin-top: 5px;overflow:auto;" plain="false"
    buttons="#tabsButtons"
>
    <div title="物料" >
   
   <div id="datagrid1" class="mini-datagrid" style="height:430px;width: 100%;" 
        url="" idField="id" showPager='false'
        allowResize="true" 
        allowCellEdit="true" allowCellSelect="true" multiSelect="true"
        allowCellValid="true" oncellvalidation="" 
    >
    
        <div property="columns">
           
             <div field="Item"  headerAlign="center"  >物料号
                <input property="editor" class="mini-textbox" style="width:100%;" />
            </div>    
            
             <div field="ItemName"  headerAlign="center" width='300px' >物料名称
                <input property="editor" class="mini-textbox" style="width:100%;" />
            </div>   
            <div field="U_TypeOption"  headerAlign="center" width='80px' >颜色
                <input property="editor" class="mini-textbox" style="width:100%;" />
            </div> 
            
              
            <div field="ItemCount"  headerAlign="center"  >数量
                <input property="editor" class="mini-textbox" style="width:100%;" />
                 <input property="editor" class="mini-spinner"  minValue="0" maxValue="999999" />
            </div>        
            
            <div field="Sprice"  headerAlign="center"  >价格
                <input property="editor" class="mini-spinner"  minValue="0" maxValue="9999999" />
            </div> 
          
        </div>
    </div>
    
    </div>
    <!-- <div title="Tab2" iconCls="" >
        2
    </div> -->
</div>
<br/>
	备注: <input id="textarea1" name="Area" class="mini-textarea" style="width: 300px;height: 100px" />
  <div id='editForm1'>
    <br/>
    <a class="mini-button" onclick="onButtonEdit()" enabled="" id='query-bt' >&nbsp;&nbsp;添加物料&nbsp;&nbsp;</a>
    <a class="mini-button" onclick="save()" enabled="" id='query-bt' >&nbsp;&nbsp;保存&nbsp;&nbsp;</a>
  </div>
</body>
<script type="text/javascript">
        mini.parse();
        var grid = mini.get("datagrid1");
        grid.on("drawcell", function (e) {
        var record = e.record,
        column = e.column,
        field = e.field,
        value = e.value;

            //格式化日期
            if (field == "U_FroTime"||field=='U_ToTime') {
                if (mini.isDate(value)) e.cellHtml = mini.formatDate(value, "yyyy-MM-dd HH:mm:ss");
				
            }

          

        });

       

        function onButtonEdit(e) {
            var btnEdit = this;
            mini.open({
                url:  "/sap/goods_select_sap.php",
                title: "选择物料",
                width: 650,
                height: 380,
                ondestroy: function (action) {
                    //if (action == "close") return false;
                    if (action == "ok") {
                        var iframe = this.getIFrameEl();
                        var data = iframe.contentWindow.GetData();
                        data = mini.clone(data);    //必须
                        if (data) {
                        	data.Item=data.catid;
                        	data.ItemName=data.name;
                        	data.Sprice=data.price;
                        	data.ItemCount = 0;
                            grid.addRow(data, 0);
                           
                        }
                    }

                }
            });
        }

        function save() {
        	var data = grid.getChanges();
        	if(data.length==0){
        		mini.alert('请添加物料');
        		return ;
        	}
			for(var i=0;i<data.length;i++){
				if(data[i].ItemCount<=0){
					mini.alert('['+data[i].ItemName+']'+'  数量不能小于等于0');
					return ;
				}
			}
			

           var form = new mini.Form("#fm1");
           var json = mini.encode(data); 
           var type = mini.get('s_type').getText();
           if(type==''){
        	   mini.alert('请选择入库类型');
        	   return ;
           }
           
           var CardCode = mini.get('CardCode').getText();
			 var CardName = mini.get('CardCode').getValue();
			 if(type=='退货入库'){
		     if(CardCode==''){
		    	 mini.alert('供应商代码不能为空');
		    	 return ; 
		     }
			 }
           
           var remark = mini.get('textarea1').getValue();
           
             mini.mask({
                el: document.body,
                cls: 'mini-mask-loading',
                html: '保存中...'
            }); 
             var type = mini.get('s_type').getValue(); //type 改成获取代码code
            $.ajax({
                url: "/warehouse/saveenter",
                data: {CardName:CardName,CardCode:CardCode, data: json,type:type,remark:remark},
                type: "post",
                dataType:'json',
                success: function (data) {
                   if(data.Success){
                	    mini.unmask(document.body);
						mini.alert(data.Description);
						search();
						return ;
                   }else{
                	   mini.unmask(document.body);
                	   mini.alert(data.Description);
                       }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(jqXHR.responseText);
                }
            });
        }


    </script>
    </block>

